package database

import (
	"xorm.io/xorm"
)

// Session 事物管理
func Session(session *xorm.Session, callback func(s *xorm.Session) error) error {
	defer func(s *xorm.Session) {
		_ = s.Close()
	}(session)
	_ = session.Begin()

	if err := callback(session); err != nil {
		_ = session.Rollback()
		return err
	}

	if err := session.Commit(); err != nil {
		_ = session.Rollback()
		return err
	}
	return nil
}
